package com.yl.ydk.presenter.impl

import android.content.Context
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.GoodsDetailBean
import com.yl.ydk.bean.body.GoodsDetailBody
import com.yl.ydk.presenter.contract.HealthyGoodsDetailContract
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class HealthyGoodsDetailImpl : BasePresenter<HealthyGoodsDetailContract.View>(),
    HealthyGoodsDetailContract.Presenter<HealthyGoodsDetailContract.View> {

    override fun getGoodsDetail(context: Context, body: GoodsDetailBody) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getGoodsDetail(body)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<GoodsDetailBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: GoodsDetailBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {


                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }

}